package com.auxgroup.smarthome.controller;


import com.auxgroup.smarthome.basebean.MenuTypeEnum;
import com.auxgroup.smarthome.dto.RoleDto;
import com.auxgroup.smarthome.filter.annotation.PermissionMenu;
import com.auxgroup.smarthome.service.RoleService;
import com.auxgroup.smarthome.session.SessionRole;
import com.auxgroup.smarthome.web.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;


@Api(value = "角色接口", tags = "系统管理")
@RestController
@RequestMapping(value = "/upms")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "角色名是否存在",notes = "查询角色名称是否重名-创建与修改角色的时候前端异步请求")
    @GetMapping(value = "/role/is_exists_name")
    @PermissionMenu(id = "upms_role_isexist", parentId = "umps_menu_role_manage", name = "角色是否存在", menuType = MenuTypeEnum.BUTTON)
    public ApiResponse isExistRepeateName(@RequestParam @Valid @NotNull(message = "角色名称不能为空") String roleName){
        return roleService.isExistRName(roleName);
    }


    @ApiOperation(value = "创建角色")
    @PostMapping(value = "/role")
    @PermissionMenu(id = "upms_role_createRole", parentId = "umps_menu_role_manage", name = "创建角色", menuType = MenuTypeEnum.BUTTON)
    public ApiResponse create(@RequestBody RoleDto roleDto) {
        return roleService.createRole(roleDto);
    }

    @ApiOperation(value = "删除角色")
    @DeleteMapping(value = "/roles/{id}")
    @PermissionMenu(id = "upms_role_delete", parentId = "umps_menu_role_manage", name = "删除角色", menuType = MenuTypeEnum.BUTTON)
    public ApiResponse delete(@PathVariable("id") String id) {
        return roleService.deleteRoleById(id);
    }

    @ApiOperation(value = "更新角色")
    @PutMapping(value = "/roles/{roleId}")
    @PermissionMenu(id = "upms_role_updateRole", parentId = "umps_menu_role_manage", name = "更新角色", menuType = MenuTypeEnum.BUTTON)
    public ApiResponse update(@PathVariable("roleId") String roleId, @RequestBody RoleDto roleDto) {
        return roleService.updateRole(roleId, roleDto);
    }

    @ApiOperation(value = "查看角色详情")
    @GetMapping(value = "/roles/{roleId}")
    @PermissionMenu(id = "upms_role_showone", parentId = "umps_menu_role_manage", name = "查看角色", menuType = MenuTypeEnum.BUTTON)
    public ApiResponse<SessionRole> getUserById(@PathVariable("roleId") String roleId) {
        return ApiResponse.ok(roleService.getRolebyId(roleId));
    }

    @ApiOperation(value = "角色列表(不分页)",notes = "当前列表不分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keywords", value = "关键字", paramType = "query", dataType = "string")
    })
    @GetMapping(value = "/roles")
    @PermissionMenu(id = "upms_role_data_list", parentId = "umps_menu_role_manage", name = "角色列表", menuType = MenuTypeEnum.BUTTON)
    public ApiResponse<List<SessionRole>> showRoleList(String keywords) {
        List<SessionRole> allRoles = roleService.getAllRole(keywords);
        return ApiResponse.ok(allRoles);
    }


}
